﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LabMS.Web.Equipment.Valuables
{
    public partial class valuablesreturn : Base.BasePage
    {
        public string strID = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("GZSBLY");//必须写在ispostback外
            strPageUserType.Add("mana");

            tbReturnDate.Attributes.Add("onclick", "c.showMoreDay = false;c.show(this);");
            tbReturnDate.Attributes.Add("readOnly", "-1");

            if (Request.QueryString["id"] == null)
            {
                Common.JShelper.JSAlert(Page, "err", "参数错误!");
                return;
            }
            strID = Request.QueryString["id"].ToString();
            if (!Page.IsPostBack)
            {
                LabMS.Model.Recipients mrecipients = checkQueryString(strID);
                if (mrecipients == null)
                {
                    Common.JShelper.JSAlert(Page, "err2", "数据不存在!");
                    return;
                }
                bindDDL();
                Bind(mrecipients);

                lbPageName.Text = mrecipients.Name + "--归还";
                lbCID.Text = mrecipients.ID.ToString();
            }
        }

        LabMS.Model.Recipients checkQueryString(string strID)
        {
            LabMS.BLL.Recipients brecipients = new LabMS.BLL.Recipients();
            string strWhere = " Type = 'guizhongyiqi' and ID =" + strID;
            List<LabMS.Model.Recipients> listrecipients = brecipients.GetModelList(strWhere);
            if (listrecipients.Count <= 0)
            {
                return null;
            }
            return listrecipients[0];
        }

        public void bindDDL()
        {
            ExtendBLL.Dictionary eDictionary = new ExtendBLL.Dictionary();
            List<Model.Dictionary> listCClass = new List<LabMS.Model.Dictionary>();
            listCClass = eDictionary.GetItemListByDClass("estatus");
            ddlRStatus.DataSource = listCClass;
            ddlRStatus.DataTextField = "Name";
            ddlRStatus.DataValueField = "Code";
            ddlRStatus.DataBind();
        }

        public void Bind(LabMS.Model.Recipients mrecipients)
        {
            tbCNub.Text = mrecipients.Numbers.ToString();
            tbEncodedTime.Text = mrecipients.EncodedTime.Value.ToShortDateString();
            tbRemarks.Text = mrecipients.Comment.Trim().ToString();
            tbRPersion.Text = mrecipients.MRecipients;
            tbStatus.Text = mrecipients.BorrowedState;
            lbCID.Text = mrecipients.ID.ToString();
            lbCName.Text = mrecipients.Name.ToString();
            lbOperator.Text = mrecipients.Operator.ToString();
        }

        protected void Submit_Click(object sender, EventArgs e)
        {
            LabMS.BLL.Recipients brecipients = new LabMS.BLL.Recipients();
            LabMS.Model.Recipients mrecipients = new LabMS.Model.Recipients();
            ExtendBLL.Recipients ebrecipients = new ExtendBLL.Recipients();
            try
            {
                mrecipients = brecipients.GetModel(long.Parse(lbCID.Text));
            }
            catch (Exception ex)
            {
                lbErr.Visible = true;
                lbErr.Text = ex.Message.ToString();
            }

            mrecipients.ReturnState = ddlRStatus.SelectedValue;
            mrecipients.ReturnTime = DateTime.Parse(tbReturnDate.Text.ToString());

            try
            {
                ebrecipients.ExtendReturn(mrecipients);
                LabMS.Common.JShelper.JSAlertAndRedirect(Page, "AddSuccess", "归还成功!", "valuablesborrowlist.aspx");
            }
            catch (Exception ex)
            {
                lbErr.Text = ex.Message.ToString();
            }
        }

    }
}
